const axios = require('axios');
const fs = require('fs');

let index = 1;
const downloadImage = (imageUrl, fileName) => {
    axios({
        method: 'get',
        url: imageUrl,
        responseType:'stream'
    })
        .then((response) => {
            response.data.pipe(fs.createWriteStream(`../瀑布流懒加载/js方案/assets/${fileName}`));
        })
        .catch((error) => {
            console.error(`Error downloading ${fileName}: ${error}`);
        });
};

const fetchAndDownloadImages = (url) => {
    axios.get(url)
        .then((response) => {
            const imageData = response.data;
            imageData.forEach((image) => {
                const fileName = index + "." + image.url.split('.').pop();
                downloadImage(image.url, fileName);
                index++;
            });
        })
        .catch((error) => {
            console.error(`Error fetching images: ${error}`);
        });
};

const apiUrl = 'https://juehackr.net/bizhis/index/views/t/ls/cc/22.html?isget=1&pg=';
async function start() {
    for (let i = 0; i < 3; i++) {
        await new Promise(resolve => setTimeout(resolve, 3000));
        fetchAndDownloadImages(apiUrl + i);
    }
}
start();